package com.wnhz.work.jy.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.solidfire.gson.Gson;
import com.wnhz.work.jy.BaseActivity;
import com.wnhz.work.jy.MyApplication;
import com.wnhz.work.jy.MyMain;
import com.wnhz.work.jy.R;
import com.wnhz.work.jy.adapter.SelectGoldAdapter;
import com.wnhz.work.jy.bean.SelectGoldBean;
import com.wnhz.work.jy.bean.TakeGold.GoodsBean;
import com.wnhz.work.jy.utils.ConfigUtil;
import com.wnhz.work.jy.utils.xutils3.MyCallBack;
import com.wnhz.work.jy.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ---提金币
 */
public class SelectGold extends BaseActivity implements View.OnClickListener {

    private ListView listView1,listView2,listView3,listView4;
    private SelectGoldAdapter adapter1,adapter2,adapter3,adapter4;

    private List<GoodsBean> list1 = new ArrayList<>();

    private TextView title;
    private SelectGoldBean selectGoldBean;
    private View tv_true;
    private View blank_lay;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_gold);

        blank_lay = findViewById(R.id.blank_lay);
        listView1 = (ListView) findViewById(R.id.listView1);
        tv_true = findViewById(R.id.tv_true);
        adapter1 = new SelectGoldAdapter(this,list1,listener1);
        listView1.setAdapter(adapter1);
        findViewById(R.id.tv_true).setOnClickListener(this);
        findViewById(R.id.tv_addAddress).setOnClickListener(this);

        setTextViewvg();
        initTirle();
        getData();
    }
    private void initTirle() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("提金币");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
    }
    private void setTextViewvg(){
        if (list1.size() > 0){
            blank_lay.setVisibility(View.GONE);
            tv_true.setVisibility(View.VISIBLE);
        }else {
            blank_lay.setVisibility(View.VISIBLE);
            tv_true.setVisibility(View.GONE);
        }


    }

    View.OnClickListener listener1 = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            int i = (int) view.getTag();
            int mynum = list1.get(i).getMyNum();
            switch (view.getId()){
                case R.id.add_img:

                    if (mynum < Integer.parseInt(list1.get(i).getNum())){
                        mynum++;
                        list1.get(i).setMyNum(mynum);
                    }else {
                        Toast.makeText(getApplication(),"不可大于最大数量",Toast.LENGTH_SHORT).show();
                    }

                    break;
                case R.id.reduce_img:

                    if (mynum>0){
                        mynum --;
                        list1.get(i).setMyNum(mynum);
                    }
                    break;
            }

            adapter1.notifyDataSetChanged();
        }
    };

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_true:
                ArrayList<GoodsBean> glist = new ArrayList<>();
                for (int i = 0; i < list1.size(); i++) {
                    if (list1.get(i).getMyNum()> 0){
                        glist.add(list1.get(i));
                    }
                }
                if (glist.size()> 0){
                    startActivity(new Intent(this, TakeGoldActivity.class).putExtra("list",glist));
                }else {
                    Toast.makeText(getApplication(),"请选择提取金币",Toast.LENGTH_SHORT).show();
                }

                break;

            case R.id.btn_main_left:
                finish();
                break;

            case R.id.tv_addAddress:
                startActivity(new Intent(SelectGold.this, MyMain.class).putExtra("Pagination",2));
                break;
        }
    }

    private static final String TAG = "SelectGold";
    private void getData(){
        Map<String,Object> map = new HashMap<>();
        map.put("userId", MyApplication.getInstance().userBean.getUserId());
        //map.put("userId", "112");
        showDialog();
        XUtil.Post(ConfigUtil.TAKEGOLD,map,new MyCallBack<String>(){

            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.e(TAG, "onSuccess:TAKEGOLD "+result );
                try {
                    JSONObject json = new JSONObject(result);
                    String re = json.optString("result");
                    if ("1".equals(re)){
                        selectGoldBean = new SelectGoldBean();
                        selectGoldBean = new Gson().fromJson(result,SelectGoldBean.class);
                        list1.clear();
                        list1.addAll(selectGoldBean.getGoods());
                        adapter1.notifyDataSetChanged();
                        setTextViewvg();
                        Log.e(TAG, "onSuccess: "+selectGoldBean.toString() );
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }

            @Override
            public void onFinished() {
                super.onFinished();
                closeDialog();
            }
        });
    }
}
